using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class SubscriptionDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public SubscriptionDA()
		{
		}

		public SubscriptionDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Subscription table.
		/// </summary>
		public  int Insert(Subscription subscription)
		{
			ValidationUtility.ValidateArgument("subscription", subscription);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", subscription.TenantID),
				new SqlParameter("@Name", subscription.Name),
				new SqlParameter("@ValidityPeriod", subscription.ValidityPeriod),
				new SqlParameter("@Rate", subscription.Rate),
				new SqlParameter("@Description", subscription.Description)
			};

			subscription.SubscriptionID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "SubscriptionInsert", parameters));
			return subscription.SubscriptionID;
		}

		/// <summary>
		/// Updates a record in the Subscription table.
		/// </summary>
		public  int Update(Subscription subscription)
		{
			ValidationUtility.ValidateArgument("subscription", subscription);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@SubscriptionID", subscription.SubscriptionID),
				new SqlParameter("@TenantID", subscription.TenantID),
				new SqlParameter("@Name", subscription.Name),
				new SqlParameter("@ValidityPeriod", subscription.ValidityPeriod),
				new SqlParameter("@Rate", subscription.Rate),
				new SqlParameter("@Description", subscription.Description)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "SubscriptionUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the Subscription table by its primary key.
		/// </summary>
		public  int Delete(int subscriptionID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@SubscriptionID", subscriptionID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "SubscriptionDelete", parameters));
		}

		/// <summary>
		/// Deletes a record from the Subscription table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "SubscriptionDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects a single record from the Subscription table.
		/// </summary>
		public  Subscription Select(int subscriptionID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@SubscriptionID", subscriptionID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "SubscriptionSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakeSubscription(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the Subscription table.
		/// </summary>
		public  List<Subscription> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "SubscriptionSelectAll", parameters))
			{
				List<Subscription> subscriptionList = new List<Subscription>();
				while (dataReader.Read())
				{
					Subscription subscription = MakeSubscription(dataReader);
					subscriptionList.Add(subscription);
				}

				return subscriptionList;
			}
		}

		/// <summary>
		/// Selects all records from the Subscription table by a foreign key.
		/// </summary>
		public  List<Subscription> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "SubscriptionSelectAllByTenantID", parameters))
			{
				List<Subscription> subscriptionList = new List<Subscription>();
				while (dataReader.Read())
				{
					Subscription subscription = MakeSubscription(dataReader);
					subscriptionList.Add(subscription);
				}

				return subscriptionList;
			}
		}

		/// <summary>
		/// Creates a new instance of the Subscription class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  Subscription MakeSubscription(SqlDataReader dataReader)
		{
			Subscription subscription = new Subscription();
			subscription.SubscriptionID = SqlClientUtility.GetInt32(dataReader, "SubscriptionID", 0);
			subscription.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			subscription.Name = SqlClientUtility.GetString(dataReader, "Name", String.Empty);
			subscription.ValidityPeriod = SqlClientUtility.GetString(dataReader, "ValidityPeriod", String.Empty);
			subscription.Rate = SqlClientUtility.GetString(dataReader, "Rate", String.Empty);
			subscription.Description = SqlClientUtility.GetString(dataReader, "Description", String.Empty);

			return subscription;
		}

		#endregion
	}
}
